package controller;

import csel.controller.enums.CommandEnum;
import csel.controller.events.Event;
import csel.controller.events.KeyPress;
import csel.controller.commandutil.CommandFactory;
import csel.controller.commandutil.CommandPool;
import csel.controller.decoders.EventDecoder;
import csel.controller.dispatchers.Dispatcher;
import csel.model.GameCommand;

/**
 * Purpose: Decodes keypresses, implements state logic for menu system
 * 
 * @author Nikolas Wolfe
 */

public class KeyEventDecoder extends EventDecoder<KeyPress>
{
	private final KeyDecoderState inventoryUse;
	private final KeyDecoderState inventoryEquip;
	private final KeyDecoderState inventoryDropUsable;
	private final KeyDecoderState inventoryDropEquippable;
	private final KeyDecoderState skillsUse;
	private final KeyDecoderState skillsUpgrade;
	private final KeyDecoderState equipmentUnequip;
	private final CommandPool pool;
	private KeyDecoderState currentState;
	
	
	public KeyEventDecoder(CommandPool pool, 
			Dispatcher disp)
	{
		super(disp);
		this.pool = pool;
		this.inventoryUse = new InventoryUseState();
		this.inventoryEquip = new InventoryEquipState();
		this.inventoryDropUsable = new InventoryDropUsableState();
		this.inventoryDropEquippable = new InventoryDropEquippableState();
		this.skillsUse = new SkillsUseState();
		this.skillsUpgrade = new SkillsUpgradeState();
		this.equipmentUnequip = new EquipmentUnequipState();
		
		// default state
		currentState = skillsUse;
	}

	/**
	 * ========================================================================
	 * All external calls from the superclass to retrieve a GameCommand are 
	 * invoked upon to this method
	 */
	@Override
	protected GameCommand parseEvent(Event event)
	{
		return currentState.parseEvent(event);
	}
	
	/**
	 * ========================================================================
	 * This is the abstract definition of a key decoder state, this implements
	 * the same interface as the context
	 */
	private abstract class KeyDecoderState 
	{
		public void activate()
		{
			currentState = this;
		}
		
		abstract protected GameCommand parseEvent( Event event );
	}
	
	/**
	 * ========================================================================
	 * Iventory Use State
	 */
	private class InventoryUseState extends KeyDecoderState
	{
		protected GameCommand parseEvent(Event event)
		{
			inventoryEquip.activate();
			return new GameCommand() 
			{
				@Override
				public void execute()
				{
					// TODO Auto-generated method stub
					System.out.println("InventoryUseState");
				}
			};
		}
	}
	
	/**
	 * ========================================================================
	 * Iventory Equip State
	 */
	private class InventoryEquipState extends KeyDecoderState
	{
		protected GameCommand parseEvent(Event event)
		{
			inventoryDropUsable.activate();
			return new GameCommand() 
			{
				@Override
				public void execute()
				{
					// TODO Auto-generated method stub
					System.out.println("InventoryEquipState");
				}
			};
		}
	}
	
	/**
	 * ========================================================================
	 * Iventory Drop Usable State
	 */
	private class InventoryDropUsableState extends KeyDecoderState
	{
		protected GameCommand parseEvent(Event event)
		{
			inventoryDropEquippable.activate();
			return new GameCommand() 
			{
				@Override
				public void execute()
				{
					// TODO Auto-generated method stub
					System.out.println("InventoryDropUsableState");
				}
			};
		}
	}
	
	/**
	 * ========================================================================
	 * Iventory Drop Equippable State
	 */
	private class InventoryDropEquippableState extends KeyDecoderState
	{
		protected GameCommand parseEvent(Event event)
		{
			skillsUse.activate();
			return new GameCommand() 
			{
				@Override
				public void execute()
				{
					// TODO Auto-generated method stub
					System.out.println("InventoryDropEquippableState");
				}
			};
		}
	}
	
	/**
	 * ========================================================================
	 * Skills Use State
	 */
	private class SkillsUseState extends KeyDecoderState
	{
		protected GameCommand parseEvent(Event event)
		{
			skillsUpgrade.activate();
			return new GameCommand() 
			{
				@Override
				public void execute()
				{
					// TODO Auto-generated method stub
					System.out.println("SkillsUseState");
				}
			};
		}
	}
	
	/**
	 * ========================================================================
	 * Skills Upgrade State
	 */
	private class SkillsUpgradeState extends KeyDecoderState
	{
		protected GameCommand parseEvent(Event event)
		{
			equipmentUnequip.activate();
			return new GameCommand() 
			{
				@Override
				public void execute()
				{
					// TODO Auto-generated method stub
					System.out.println("SkillsUpgradeState");
				}
			};
		}
	}
	
	/**
	 * ========================================================================
	 * Equipment Unequip
	 */
	private class EquipmentUnequipState extends KeyDecoderState
	{
		protected GameCommand parseEvent(Event event)
		{
			inventoryUse.activate();
			return new GameCommand() 
			{
				@Override
				public void execute()
				{
					// TODO Auto-generated method stub
					System.out.println("TAIT IS A MUTHAFUCKAS!!!!");
				}
			};
		}
	}
}
